package d001;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "d001_mytest_a001f", urlPatterns = {"/d001_mytest/a001f"})
public class a001f extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int areaCnt = 5, navCnt = 5, funcCnt = 5;
        //  ----  取得相關參數 (start) ----
        String winId = request.getParameter("winId");
        int areaId = 0;
        try {
            areaId = Integer.parseInt(request.getParameter("areaId"));
        } catch (NumberFormatException ex) {
        }
        int navId = 0;
        try {
            navId = Integer.parseInt(request.getParameter("navId"));
        } catch (NumberFormatException ex) {
            try {
                String tmpVal = (String) request.getSession().getAttribute(String.format("winId:%s,areaId:%s", winId, areaId));
                navId = Integer.parseInt(tmpVal);
            } catch (NumberFormatException ex2) {
            }
        }
        request.getSession().setAttribute(String.format("winId:%s,areaId:%s", winId, areaId), navId + "");
        //
        int funcId = 0;
        try {
            funcId = Integer.parseInt(request.getParameter("funcId"));
        } catch (NumberFormatException ex) {
            try {
                String tmpVal = (String) request.getSession().getAttribute(String.format("winId:%s,areaId:%s,navId:%s", winId, areaId, navId));
                funcId = Integer.parseInt(tmpVal);
            } catch (NumberFormatException ex2) {
            }
        }
        request.getSession().setAttribute(String.format("winId:%s,areaId:%s,navId:%s", winId, areaId, navId), funcId + "");
        //  ----  取得相關參數 ( end ) ----
        //
        StringBuilder strBldr = new StringBuilder();
        strBldr.append("<!DOCTYPE html>\n");
        strBldr.append("<html>\n");
        strBldr.append("<head>\n");
        strBldr.append("<title>TODO supply a title</title>\n");
        strBldr.append("<meta charset=\"UTF-8\">\n");
        strBldr.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
        strBldr.append("<link href=\"a001.css\" rel=\"stylesheet\" type=\"text/css\" title=\"a001_css\"/>\n");
        //
        //  ----  設定 javascript (start) ----
        strBldr.append("<script>\n");
        if (winId == null) {
            strBldr.append("winId = Math.random();\n");
        } else {
            strBldr.append(String.format("winId = %s;\n", winId));
        }
        strBldr.append("function next_step(areaId, navId, funcId) {\n");
        strBldr.append("var nxtUrl = 'a001f?winId=' + winId;\n");
        strBldr.append("if (typeof(areaId) !== \"undefined\") {\n");
        strBldr.append("nxtUrl += \"&areaId=\" + areaId;\n");
        strBldr.append("}\n");
        strBldr.append("if (typeof(navId) !== \"undefined\") {\n");
        strBldr.append("nxtUrl += \"&navId=\" + navId;\n");
        strBldr.append("}\n");
        strBldr.append("if (typeof(funcId) !== \"undefined\") {\n");
        strBldr.append("nxtUrl += \"&funcId=\" + funcId;\n");
        strBldr.append("}\n");
        strBldr.append("location.href = nxtUrl;\n");
        strBldr.append("}\n");
        strBldr.append("</script>\n");
        //  ----  設定 javascript ( end ) ----
        //
        strBldr.append("</head>\n");
        strBldr.append("<body>\n");
        strBldr.append("<div class=\"header\"><h1 class=\"header\"> Header2(中文) </h1></div>\n");
        strBldr.append("<div class=\"area\">\n");
        strBldr.append(String.format("<h1 class=\"area\">Area (中文) %s</h1>\n", areaId));
        //
        //  ----  定義 Area 部份 (start) ----
        strBldr.append("<ul class=\"area\">\n");
        for (int idx = 0; idx < areaCnt; idx++) {
            if (idx == areaId) {
                strBldr.append(String.format(" <li class=\"active_area\">AREA %s (中文)</li> \n", idx, 1));
            } else {
                strBldr.append(String.format(" <li class=\"area\"  onclick=\"javascript:next_step(%s)\">AREA %s (中文)</li> \n", idx, idx, 1));
            }
        }
        strBldr.append("</ul>\n");
        strBldr.append("</div>\n");
        //  ----  定義 Area 部份 ( end ) ----
        //
        strBldr.append("<div class=\"area_content\">\n");
        //
        //  ----  定義 Navigator 部份 (start) ----
        strBldr.append("<div class=\"navigator\">\n");
        strBldr.append(String.format("<h1 class=\"navigator\">Navigator (中文) %s</h1>\n", navId));
        strBldr.append("<ul class=\"navigator\">\n");
        for (int idx = 0; idx < navCnt; idx++) {
            if (idx == navId) {
                strBldr.append(String.format(" <li class=\"active_navigator\">Func %s (中文) </li> \n", idx, 1));
            } else {
                strBldr.append(String.format(" <li class=\"navigator\"  onclick=\"javascript:next_step(%s,%s)\">Func %s (中文)</li> \n", areaId, idx, idx, 1));
            }
        }
        strBldr.append("</ul>\n");
        strBldr.append("</div>\n");
        //  ----  定義 Navigator 部份 ( end ) ----
        //
        strBldr.append("<div class=\"navigator_content\">\n");
        //
        //  ----  定義 Func_tab 部份 (start) ----
        strBldr.append("<div class=\"func_tab\">\n");
        strBldr.append("<h1 class=\"func_tab\">Function Tab (中文)</h1>\n");
        strBldr.append("<ul class=\"func_tab\">\n");
        for (int idx = 0; idx < funcCnt; idx++) {
            if (idx == funcId) {
                strBldr.append(String.format(" <li class=\"active_func_tab\">Tab %s (中文) </li> \n", idx, 1));
            } else {
                strBldr.append(String.format(" <li class=\"func_tab\"  onclick=\"javascript:next_step(%s,%s,%s)\">Tab %s (中文)</li> \n", areaId, navId, idx, idx, 1));
            }
        }
        strBldr.append("</ul>\n");
        strBldr.append("</div>\n");
        //  ----  定義 Func_tab 部份 ( end ) ----
        //
        strBldr.append("<div class=\"func_tab_content\">\n");
        strBldr.append("<div id=\"data_content\">\n");
        strBldr.append("<h1>Test Info</h1>\n");
        strBldr.append("<ul>\n");
        strBldr.append("<li>a</li>\n");
        strBldr.append("<li>b</li>\n");
        strBldr.append("<li>c</li>\n");
        strBldr.append("</ul>\n");
        strBldr.append("</div>\n");
        strBldr.append("</div>\n");

        strBldr.append("</div>\n");

        strBldr.append("</div>\n");
        strBldr.append("<div class=\"footer\">Footer (中文)</div>\n");
        strBldr.append("</body>\n");
        strBldr.append("</html>\n");
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            out.println(strBldr.toString());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
